﻿#include "ReflectionPass.h"

#include "../../../Common/Singleton.h"
#include "../ResourceManager/Dx2MaterialManager.h"
#include "../ResourceManager/Dx2GeometryManager.h"
#include "../ResourceManager/Dx2RootSignatureManager.h"
#include "../ResourceManager/Dx2ShaderAndLayoutManager.h"


void ReflectionPass::BuildRenderItems(std::vector<std::unique_ptr<RenderItem>>& allRitems,UINT& lastObjIndex)
{
    auto skullRitem = std::make_unique<RenderItem>();
    skullRitem->World = MathHelper::Identity4x4();
    skullRitem->ObjCBIndex = lastObjIndex;
    lastObjIndex++;
    skullRitem->Mat = Singleton<Dx2MaterialManager>::Instance()->GetMaterial("skullMat").get();
    skullRitem->Geo = Singleton<Dx2GeometryManager>::Instance()->GetGeometry("skullGeo").get();
    skullRitem->PrimitiveType = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
    skullRitem->IndexCount = skullRitem->Geo->DrawArgs["skull"].IndexCount;
    skullRitem->StartIndexLocation = skullRitem->Geo->DrawArgs["skull"].StartIndexLocation;
    skullRitem->BaseVertexLocation = skullRitem->Geo->DrawArgs["skull"].BaseVertexLocation;

    mReflectedSkullRitem = skullRitem.get();

    mRenderItems.push_back(skullRitem.get());
    allRitems.push_back(move(skullRitem));
}

void ReflectionPass::BuildPSOs(ID3D12Device* device, DXGI_FORMAT BackBufferFormat, DXGI_FORMAT DepthStencilFormat,
    bool _4xMsaaState, UINT _4xMsaaQuality)
{
    D3D12_GRAPHICS_PIPELINE_STATE_DESC drawReflectionsPsoDesc;
    ZeroMemory(&drawReflectionsPsoDesc,sizeof(D3D12_GRAPHICS_PIPELINE_STATE_DESC));
	
    drawReflectionsPsoDesc.InputLayout = {Singleton<Dx2ShaderAndLayoutManager>::Instance()->GetInputLayout().data(),(UINT)Singleton<Dx2ShaderAndLayoutManager>::Instance()->GetInputLayout().size()};
    drawReflectionsPsoDesc.pRootSignature = Singleton<Dx2RootSignatureManager>::Instance()->GetRootSignature(RootSignatureType::Default).Get();
	
    drawReflectionsPsoDesc.VS =
        {
        reinterpret_cast<BYTE*>(Singleton<Dx2ShaderAndLayoutManager>::Instance()->GetShader("standardVS")->GetBufferPointer()),
        Singleton<Dx2ShaderAndLayoutManager>::Instance()->GetShader("standardVS")->GetBufferSize()
    };
	
    drawReflectionsPsoDesc.PS =
        {
        reinterpret_cast<BYTE*>(Singleton<Dx2ShaderAndLayoutManager>::Instance()->GetShader("opaquePS")->GetBufferPointer()),
        Singleton<Dx2ShaderAndLayoutManager>::Instance()->GetShader("opaquePS")->GetBufferSize()
    };
	
    drawReflectionsPsoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
    drawReflectionsPsoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
    drawReflectionsPsoDesc.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);
    drawReflectionsPsoDesc.SampleMask = UINT_MAX;
    drawReflectionsPsoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
    drawReflectionsPsoDesc.NumRenderTargets = 1;
    drawReflectionsPsoDesc.RTVFormats[0] = BackBufferFormat;
    drawReflectionsPsoDesc.SampleDesc.Count = _4xMsaaState? 4:1;
    drawReflectionsPsoDesc.SampleDesc.Quality = _4xMsaaState? (_4xMsaaQuality - 1):0;
    drawReflectionsPsoDesc.DSVFormat = DepthStencilFormat;

    D3D12_DEPTH_STENCIL_DESC reflectionsDSS;
    reflectionsDSS.DepthEnable = true;
    reflectionsDSS.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
    reflectionsDSS.DepthFunc = D3D12_COMPARISON_FUNC_LESS;
    reflectionsDSS.StencilEnable = true;
    reflectionsDSS.StencilReadMask = 0xff;
    reflectionsDSS.StencilWriteMask = 0xff;

    reflectionsDSS.FrontFace.StencilFailOp = D3D12_STENCIL_OP_KEEP;
    reflectionsDSS.FrontFace.StencilDepthFailOp = D3D12_STENCIL_OP_KEEP;
    reflectionsDSS.FrontFace.StencilPassOp = D3D12_STENCIL_OP_KEEP;
    reflectionsDSS.FrontFace.StencilFunc = D3D12_COMPARISON_FUNC_EQUAL;

    reflectionsDSS.BackFace.StencilFailOp = D3D12_STENCIL_OP_KEEP;
    reflectionsDSS.BackFace.StencilDepthFailOp = D3D12_STENCIL_OP_KEEP;
    reflectionsDSS.BackFace.StencilPassOp = D3D12_STENCIL_OP_KEEP;
    reflectionsDSS.BackFace.StencilFunc = D3D12_COMPARISON_FUNC_EQUAL;

    drawReflectionsPsoDesc.DepthStencilState = reflectionsDSS;
    drawReflectionsPsoDesc.RasterizerState.CullMode = D3D12_CULL_MODE_BACK;
    drawReflectionsPsoDesc.RasterizerState.FrontCounterClockwise = true;

    ThrowIfFailed(device->CreateGraphicsPipelineState(&drawReflectionsPsoDesc,IID_PPV_ARGS(&mPso)));
}

void ReflectionPass::Draw(ID3D12GraphicsCommandList* cmdList, CD3DX12_GPU_DESCRIPTOR_HANDLE hGpuHandler,
    UINT srvHeapSize, UINT objCBByteSize, UINT matCBByteSize, UINT passCBByteSize, 
    ID3D12Resource* objectCB,ID3D12Resource* matCB, ID3D12Resource* passCB,
    const PassConstants& passValue,
    ID3D12Resource* OutColorRenderTarget,
    D3D12_CPU_DESCRIPTOR_HANDLE OutColorRenderTargetView,
    D3D12_CPU_DESCRIPTOR_HANDLE OutDepthStentilView
 
    )
{
    cmdList->SetPipelineState(mPso.Get());
    cmdList->SetGraphicsRootConstantBufferView(2, passCB->GetGPUVirtualAddress() + passCBByteSize);
    DrawRenderItem(cmdList,hGpuHandler,srvHeapSize,objCBByteSize,matCBByteSize,objectCB,matCB,mRenderItems);
}
